/**
 * @filename:UserServiceImpl 2019年4月9日
 * @project user_center  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd. 
 * All right reserved. 
 */
package com.association.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.association.mapper.DeptMapper;
import com.association.model.entity.Dept;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.len.base.impl.BaseServiceImpl;
import com.association.model.entity.User;
import com.association.mapper.UserMapper;
import com.association.service.UserService;

/**   
 *  
 * @Description:  用户——SERVICEIMPL
 * @Author:       BianPeng   
 * @CreateDate:   2019年4月9日
 * @Version:      V1.0
 *    
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, String> implements UserService {
	
	@Resource
	UserMapper userMapper;
	@Resource
	DeptMapper deptMapper;


	//查询对象
	@Override
	public User selectByPrimaryKey(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}
	
	//删除对象
	@Override
	public int deleteByPrimaryKey(Long id) {
		return userMapper.deleteByPrimaryKey(id);
	}
	
	//添加对象
	@Override
	public int insertSelective(User user) {
		return userMapper.insertSelective(user);
	}
	
	//修改对象
	@Override
	public int updateByPrimaryKeySelective(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}
	
	//查询集合
	@Override
	public List<User> queryUserList(User user) {
		if(user.getRoleId()!=null){
			ArrayList<Long> longs = new ArrayList<>();
			user.setIds(deptListId(longs, user.getRoleId()));
		}
		List<User> list = userMapper.queryUserList(user);
		list.forEach(res->{
			res.setDeptName(deptMapper.selectById(res.getDeptId()).getName());
			res.setProjectName(deptMapper.selectById(res.getProjectId()).getName());
			res.setRoleName(deptMapper.selectById(res.getRoleId()).getName());
		});
		return list;
	}

	public List<Long> deptListId(List<Long> ids, long id){
		Dept dept = deptMapper.selectById(id);
		ids.add(dept.getId());

		List<Dept> depts = deptMapper.selectList(new QueryWrapper<Dept>().eq("pid",id));
		depts.forEach(res->{
			Long deptPid = res.getId();
			deptListId(ids, deptPid);
		});
		return ids;
	}

}